import scrapy
from scrapy import Selector
from zhongyaocai.items import ZhongyaocaiItem
import time
import requests
import json


from pymongo import MongoClient

# 连接到MongoDB（默认连接到localhost的27017端口）
client = MongoClient('mongodb://yaocai:yaocaiAdmin@localhost:27017/?authSource=zhongyao')

# 选择数据库，如果不存在则会自动创建
db = client['zhongyao']

# 选择集合（如果不存在也会自动创建）
collection = db['yaocai']

class ZhongyaocaiSpiderSpider(scrapy.Spider):
    name = "zhongyaocai_spider"
    allowed_domains = ["www.zysj.com.cn"]
    start_urls = ["https://www.zysj.com.cn/zhongyaocai/index.html"]


    def parse(self, response):

        selector = Selector(response)

        print("--------------------------")
        # item['title'] = response.xpath('//a[@class="titlelnk"]/text()').extract()       #使用xpath搜索
        # item['link'] = response.xpath('//a[@class="titlelnk"]/@href').extract()
        lis = selector.xpath('//*[@id="filter"]/ul[2]/li/ul/li/a[contains(@href,"/zhongyaocai/index")]')
        # print(lis)
        # liElements = lis.getall()
        for index,  li in enumerate(lis):
            item = ZhongyaocaiItem()
            # print(li)
            href = li.xpath("@href").get()
            item["url"] = href
            text = li.xpath("text()").get()
            item["letter"] = text

            item["title"] = "拼音"
            # print(href,text)
            yield item
        # pass
